package com.example.c3p0.dao;

import com.example.c3p0.Util.C3p0Util;
import com.example.c3p0.javabean.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Queue;

/**
 * @auther 尚智江
 * @Date 2022/5/15 0:08
 */
public class UserDaoImpl implements UserDao{
    public User insertUser(User user) throws SQLException {
        QueryRunner queryRunner = new QueryRunner(C3p0Util.getDataSource());
        String sql = "INSERT INTO user VALUES (?,?)";
        int num = queryRunner.update(sql,user.getUsername(),user.getPassword());
        if (num > 0){
            return user;
        }else return null;
    }
    public Boolean login(String username,String password) throws SQLException{
        //创建QueryRunner对象
        QueryRunner runner=new QueryRunner(C3p0Util.getDataSource());
        //写SQL语句
        String sql="select count(*) from user where username=? and password=?";
        Object query = runner.query(sql,new ScalarHandler<Object>(),new Object[]{username,password});
        int count=((Long)query).intValue();
        if(count==1){
            return true;
        }else{
            return false;
        }
    }
//    @Override
//    public User selectPasswordByUser(User user) throws SQLException {
//        QueryRunner queryRunner = new QueryRunner(C3p0Util.getDataSource());
//        String sql = "SELECT * FROM user";
//        user = queryRunner.query(sql,new BeanHandler<>(User.class));
//        return user;
//    }
//    public List<User> selectAll()throws SQLException{
//        List<User> list = null;
//        QueryRunner queryRunner = new QueryRunner(C3p0Util.getDataSource());
//        String sql = "";
//        list = queryRunner.query(sql,new BeanListHandler<User>(User.class));
//        return list;
//    }
//    public User selectPasswordByUser(String username)throws SQLException{
//        User user = null;
//        QueryRunner queryRunner = new QueryRunner(C3p0Util.getDataSource());
//        String sql = "SELECT password FROM user WHERE user = ?";
//        user = queryRunner.query(sql,new BeanHandler<User>(User.class),username);
//        return user;
//        String password = null;
//        QueryRunner queryRunner = new QueryRunner(C3p0Util.getDataSource());
//        String sql = "SELECT password FROM user WHERE user = ?";
//        int num = queryRunner.update(sql,user);
//        if(num>0){
//            return user;
//        }
//        return queryRunner.query(sql,new BeanHandler<User>(User.class),user);
//        password = queryRunner.query(sql,new BeanHandler<String>(String.class),user);
//        return password;
//    }

}
